//
//  GiftSprite.m
//  isMan
//
//  Created by Xing Lion on 11-6-21.
//  Copyright 2011年 InspurWorld. All rights reserved.
//

#import "GiftSprite.h"
#import "HelloWorldLayer.h"
#import "Fly.h"

@implementation GiftSprite
@synthesize gamely = _gamely;
@synthesize gifttype = _gifttype;

-(void)getGift
{

    HelloWorldLayer* ly = (HelloWorldLayer*)_gamely;
    
    CGRect flyRect =  [ly.fly getspriteRect];
    
   CGRect rc = CGRectMake(self.position.x - self.textureRect.size.width / 2, self.position.y - self.textureRect.size.height /2, self.textureRect.size.width, self.textureRect.size.height);
    
    
    if (CGRectIntersectsRect(flyRect, rc))
    {
       
        // fly change
        
       // [self unschedule:@selector(getGift)];
        
        [self setVisible:NO];
       // [self stopAllActions];
        

        [ly setGift:_gifttype];
    }
    else
    {
    
        // fly 
    }

}

-(void)SetGamelayer:(CCLayer*)ly
{

    _gamely = ly;
}


+ (id) gitfWithinLayer:(CCLayer *)layer
{

	HelloWorldLayer *ly = (HelloWorldLayer *)layer;
    
	GiftSprite *sprite = [GiftSprite spriteWithFile:@"Chansey.png" rect:CGRectMake(0, 0, 30, 30)];
	[ly addChild:sprite z:100];
    
	[sprite setVisible:NO];
	[sprite SetGamelayer:layer];
    
	return sprite;
}

-(id)init
{

	if( (self=[super init] )) 
    {
        if( (self=[super init] )) 
        {
            
           [self schedule:@selector(getGift) interval: 1/60];
            _gifttype = 0;
            
        }
        
        return self;
        
    }
    
    return self;
}


@end
